/**
 *  @file Event.cpp
 */
#include "../common/CppMemPool.h"
#include "Event.h"
#include "Semaphore.h"

namespace cppflib
{

namespace threading
{


Event::Event(void)
{
   Semaphore *pSem = new Semaphore(1); 
   cppmempool::Retain(pSem);
   this->pSem = static_cast<BaseObject*>(pSem);
}

Event::Event(pcwstr_t name)
{
   Semaphore *pSem = new Semaphore(name, 1); 
   cppmempool::Retain(pSem);
   this->pSem = static_cast<BaseObject*>(pSem);
}

Event::~Event(void)
{
   Close();
}

void Event::Set(void)
{
   (static_cast<Semaphore*>(pSem))->Post();
}

void Event::Wait(void)
{
   (static_cast<Semaphore*>(pSem))->Wait();
}

bool Event::Wait(int timeout)
{
   return (static_cast<Semaphore*>(pSem))->Wait(timeout);
}

void Event::Close(void)
{
   if (pSem) {
      cppmempool::Release(pSem);
      pSem = NULL;
   }
}


} // end of namespace threading


} // end of namespace cppflib
